package com.tunan.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.tunan.lsj.interceptor.TokenSecurityInterceptor;

@Configuration
@ComponentScan(basePackages = "com.tunan")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
	@Bean
	public TokenSecurityInterceptor createTokenSecurityInterceptor() {
		return new TokenSecurityInterceptor();
	}

	/**
	 * 配置拦截器
	 * 
	 * @author lance
	 * @param registry
	 */
	public void addInterceptors(InterceptorRegistry registry) {
		//registry.addInterceptor(createTokenSecurityInterceptor()).addPathPatterns("/ajax/**");
	}
    /**
     * 视图设置
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".jsp");
        return resolver;
    }

    /**
     * 扩展已至此上传进度监控
     */
//    @Bean
//    public MultipartResolver multipartResolver() {
//        CommonsMultipartResolver resolver = new FileUploadMultipartResolver();
//        resolver.setMaxUploadSize(1000000000);
//        return resolver;
//    }
    
    /**
     * 错误信息国际化配置
     */
//    @Bean(name = "messageSource")
//    public MessageSource messageSource() {
//        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//        messageSource.setBasename("/WEB-INF/i18n/messages");
//        messageSource.setCacheSeconds(5);
//        return messageSource;
//    }

    
    /**
     * sitemeshFilter过滤器配置
     * @return
     */
//    @Bean(name = "sitemeshFilter")
//    public FilterRegistrationBean sitemeshFilter() {
//        FilterRegistrationBean bean = new FilterRegistrationBean();
//        bean.setFilter(new SitemeshFilter());
//        bean.addUrlPatterns("/*");
//        bean.setOrder(1111);
//        return bean;
//    }
    
    /**
     * 解决springmvc 控制器接收不了put请求参数问题
     * @return
     */
    @Bean(name = "HttpMethodFilter")
    public FilterRegistrationBean httpMethodFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new HttpPutFormContentFilter());
        bean.addUrlPatterns("/*");
        return bean;
    }
    
    /**
     * 页面登录验证码Servlet配置
     * @return
     */
/*    @Bean(name = "captchaServlet")
    public ServletRegistrationBean captchaServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new CaptchaServlet());
        bean.addUrlMappings("/captcha");
        return bean;
    }*/
    
    /**
     * mvc传送验证并转换数据类型
     */
    @Bean
    public RequestMappingHandlerAdapter handlerAdapter() {
    	RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
    	//自定义转换类型（数据）
    	adapter.setWebBindingInitializer(new DataBingding());
    	List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
    	list.add(new ByteArrayHttpMessageConverter());
    	StringHttpMessageConverter shm = new StringHttpMessageConverter();
    	shm.setWriteAcceptCharset(false);
    	list.add(shm);
    	list.add(new AllEncompassingFormHttpMessageConverter());
    	list.add(new MappingJackson2HttpMessageConverter());
    	//自定义消息转换类
    	adapter.setMessageConverters(list);
    	return adapter;
    }
}
